package com.breeze.blockchain.socket.config;

import com.breeze.blockchain.socket.WebSocketController;
import com.breeze.components.socket.WebSocketHeartBeatChecker;
import jakarta.annotation.Resource;
import lombok.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author breeze
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

  public static final String SOCKET_PATH = "/ws";
  public static final String SESSION_ID = "sessionId";

  @Resource WebSocketController webSocketController;
  @Resource WebSocketInterceptor webSocketInterceptor;

  @Bean
  public WebSocketHeartBeatChecker webSocketHeartBeatChecker() {
    return new WebSocketHeartBeatChecker();
  }

  @Override
  public void registerWebSocketHandlers(@NonNull WebSocketHandlerRegistry registry) {
    registry
        .addHandler(webSocketController, "/ws")
        .addInterceptors(webSocketInterceptor)
        .setAllowedOrigins("*");
  }
}
